
source('read_dataset.R')

# for education, sex, race,
# give prop by campaign--cycle--nat_quant90/10

out = rbindlist(list(
  df12[, .(cycle = 2012, type = 'educ', dem = sum(d_val), rep = sum(r_val), dem_n = sum(d_ind), rep_n = sum(r_ind)), .(bottom90=as.numeric(as.character(nat_quant)) < 0.9, level = education)],
  df16[, .(cycle = 2016, type = 'educ',  dem = sum(d_val), rep = sum(r_val), dem_n = sum(d_ind), rep_n = sum(r_ind)), .(bottom90=as.numeric(as.character(nat_quant)) < 0.9, level = education)],
  df20[, .(cycle = 2020, type = 'educ',  dem = sum(d_val), rep = sum(r_val), dem_n = sum(d_ind), rep_n = sum(r_ind)), .(bottom90=as.numeric(as.character(nat_quant)) < 0.9, level = education)],
  
  df12[ethnicity == 'W', .(cycle = 2012, type = 'educ_w', dem = sum(d_val), rep = sum(r_val), dem_n = sum(d_ind), rep_n = sum(r_ind)), .(bottom90=as.numeric(as.character(nat_quant)) < 0.9, level = education)],
  df16[ethnicity == 'W', .(cycle = 2016, type = 'educ_w',  dem = sum(d_val), rep = sum(r_val), dem_n = sum(d_ind), rep_n = sum(r_ind)), .(bottom90=as.numeric(as.character(nat_quant)) < 0.9, level = education)],
  df20[ethnicity == 'W', .(cycle = 2020, type = 'educ_w',  dem = sum(d_val), rep = sum(r_val), dem_n = sum(d_ind), rep_n = sum(r_ind)), .(bottom90=as.numeric(as.character(nat_quant)) < 0.9, level = education)],
  
  df12[, .(cycle = 2012, type = 'sex', dem = sum(d_val), rep = sum(r_val), dem_n = sum(d_ind), rep_n = sum(r_ind)), .(bottom90=as.numeric(as.character(nat_quant)) < 0.9, level = gender)],
  df16[, .(cycle = 2016, type = 'sex',  dem = sum(d_val), rep = sum(r_val), dem_n = sum(d_ind), rep_n = sum(r_ind)), .(bottom90=as.numeric(as.character(nat_quant)) < 0.9, level = gender)],
  df20[, .(cycle = 2020, type = 'sex',  dem = sum(d_val), rep = sum(r_val), dem_n = sum(d_ind), rep_n = sum(r_ind)), .(bottom90=as.numeric(as.character(nat_quant)) < 0.9, level = gender)],
  
  df12[, .(cycle = 2012, type = 'eth', dem = sum(d_val), rep = sum(r_val), dem_n = sum(d_ind), rep_n = sum(r_ind)), .(bottom90=as.numeric(as.character(nat_quant)) < 0.9, level = ethnicity)],
  df16[, .(cycle = 2016, type = 'eth',  dem = sum(d_val), rep = sum(r_val), dem_n = sum(d_ind), rep_n = sum(r_ind)), .(bottom90=as.numeric(as.character(nat_quant)) < 0.9, level = ethnicity)],
  df20[, .(cycle = 2020, type = 'eth',  dem = sum(d_val), rep = sum(r_val), dem_n = sum(d_ind), rep_n = sum(r_ind)), .(bottom90=as.numeric(as.character(nat_quant)) < 0.9, level = ethnicity)]
))

out = out[ ! level %in% c('', 'Unkn') ]

out = rbindlist(list(
  'val' = melt(out, 1:4, measure.vars = 5:6, variable.name = 'party'),
  'n' = melt(out, 1:4, measure.vars = 7:8, variable.name = 'party')[, party:= substr(party, 1, 3)]
), idcol = 'var')


out[, cand := fcase(
  cycle == 2012 & party == 'dem', 'Obama12',
  cycle == 2012 & party == 'rep', 'Romney12',
  cycle == 2016 & party == 'dem', 'Clinton16',
  cycle == 2016 & party == 'rep', 'Trump16',
  cycle == 2020 & party == 'dem', 'Biden20',
  cycle == 2020 & party == 'rep', 'Trump20'
)]

out[type %in% c('educ', 'educ_w'), level := fifelse(level %in% c('Bach', 'Grad'), 'College', 'NoCollege')]
out = out[, .(value = sum(value)), .(var, bottom90, level, cycle, type, party, cand)]

out[, percs := value/sum(value), .(var, bottom90, cycle, type, party, cand)]

# Now need to reorder (gender, race, education)

out[, level := fcase(
  level == 'M', 'Male',
  level == 'F', 'Female',
  level == 'W', 'White',
  level == 'B', 'Black',
  level == 'H', 'Hispanic',
  level == 'A', 'Asian',
  level == 'O', 'Other',
  level == 'College' & type == 'educ', 'CollegeDegree',
  level == 'NoCollege' & type == 'educ', 'NoCollegeDegree',
  level == 'College' & type == 'educ_w', 'CollegeWhite',
  level == 'NoCollege' & type == 'educ_w', 'NoCollegeWhite'
)]

out[, level := factor(level, levels = c("Male", "Female", "White", "Black" , "Hispanic", "Asian" , "Other", "CollegeDegree" , "NoCollegeDegree", "CollegeWhite", "NoCollegeWhite"))]
out = out[order(level)]

setnames(out, 'level', 'Demographics')

out[, cand := factor(cand, levels = c('Obama12', 'Romney12', 'Clinton16', 'Trump16', 'Biden20', 'Trump20'))]

save(out, file = 'summary_data/tablesA6_A7_A8_A9.rda')

out[, cand := factor(cand, levels = c('Obama12', 'Romney12', 'Clinton16', 'Trump16', 'Biden20', 'Trump20'))]